package com.lc.luzhengtang.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.SparseArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.MessageAdapter;
import com.lc.luzhengtang.adapter.NewsListTestAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.NewsBean;
import com.lc.luzhengtang.conn.ConnDeleteNews;
import com.lc.luzhengtang.conn.ConnFlagNews;
import com.lc.luzhengtang.db.BaseTable;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/5/3.
 */

public class NewsListActivity_BeiFen2 extends BaseActivity implements View.OnClickListener, NewsListTestAdapter.CheckInterface, NewsListTestAdapter.ModifyCountInterface, SwipeRefreshLayout.OnRefreshListener,MessageAdapter.CheckInterface {

    public static final String MESSAGE_RECEIVER = "receiveMessageBoradcast";
    private CheckBox ck_all;
    private ListView list_shopping_cart;
    private NewsListTestAdapter shoppingCartAdapter;
    private TextView tv_edit;
    private boolean flag = false;
    private List<NewsBean.DataBeanX.DataBean> shoppingCartBeanList = new ArrayList<>();
    private boolean mSelect;

    RelativeLayout rl_bottom;
    /**
     * 批量模式下，用来记录当前选中状态
     */
    private SparseArray<Boolean> mSelectState = new SparseArray<Boolean>();
    public int select = 0;

    private TextView text_biaoji;

    private TextView text_flag;
    //删除按钮
    private TextView tv_settlement;
    private ImageView iv_back;
    SwipeRefreshLayout swipeLayout;

    private List<BaseTable.BaseA> list = new ArrayList<>();
    private MessageAdapter adapter;

    BroadcastReceiver receiveMessageBoradcast = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            loadDate();

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newslist);
        ButterKnife.bind(this);
        list_shopping_cart = (ListView) findViewById(R.id.list_notice);
      /*  mDatas = new ArrayList<>();
        rlBottom.setVisibility(View.GONE);*/
        rl_bottom = (RelativeLayout) findViewById(R.id.rl_bottom);
        rl_bottom.setVisibility(View.GONE);
        text_biaoji = (TextView) findViewById(R.id.text_biaoji);
        text_biaoji.setOnClickListener(this);
        text_flag = (TextView) findViewById(R.id.text_flag);
        text_flag.setOnClickListener(this);

        ck_all = (CheckBox) findViewById(R.id.ck_all);
        ck_all.setOnClickListener(this);
        tv_settlement = (TextView) findViewById(R.id.tv_settlement);
        tv_settlement.setOnClickListener(this);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));


        registerReceiver(receiveMessageBoradcast, new IntentFilter(MESSAGE_RECEIVER));

        loadDate();

    }


    private void loadDate() {


    }

    public void setRefreshing(final boolean refreshing) {
        swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(refreshing);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        onRefresh();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //全选按钮
            case R.id.ck_all:
                if (list.size() != 0) {
                    if (ck_all.isChecked()) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setChoosed(true);
                        }
                        adapter.notifyDataSetChanged();
                    } else {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setChoosed(false);
                        }
                        adapter.notifyDataSetChanged();
                    }
                }

                break;


            case R.id.text_biaoji:

                text_flag.setVisibility(View.VISIBLE);
                text_biaoji.setVisibility(View.GONE);
                rl_bottom.setVisibility(View.VISIBLE);
//                startActivity(new Intent(NewsListActivity.this, EditNewsListActivity.class));
                select = 1;
                onRefresh();

                break;

            //进行标记
            case R.id.text_flag:
                select = 0;

                StringBuilder sb2 = new StringBuilder();
                for (int r = 0; r < list.size(); r++) {

                    if (list.get(r).isChoosed()) {

                        sb2.append(list.get(r).push_id);

                        sb2.append(",");
                    }
                }

                if (sb2.length() < 1) {
                    LovelyToast.makeText(NewsListActivity_BeiFen2.this, "请选择消息", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {

                    sb2.deleteCharAt(sb2.length() - 1);
                    String id = sb2.toString();

                    new ConnFlagNews(id, new AsyCallBack<CommenBean>() {
                        @Override
                        public void onStart(int type) throws Exception {
                            super.onStart(type);
                        }

                        @Override
                        public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                            super.onSuccess(toast, type, o, commenBean);
                            LovelyToast.makeText(NewsListActivity_BeiFen2.this, commenBean.getMessage(), 0, LovelyToast
                                    .SUCCESS, LovelyToast.SCALE);
                            select = 0;
                            onRefresh();
                        }

                        @Override
                        public void onFail(String toast, int type) throws Exception {
                            super.onFail(toast, type);
                        }

                        @Override
                        public void onEnd(String toast, int type) throws Exception {
                            super.onEnd(toast, type);
                        }
                    }

                    ).execute(NewsListActivity_BeiFen2.this, false);

                }
//                onRefresh();
                break;

            case R.id.tv_settlement:
                StringBuilder sb = new StringBuilder();
                for (int r = 0; r < list.size(); r++) {

                    if (list.get(r).isChoosed()) {

                        sb.append(list.get(r).push_id);

                        sb.append(",");
                    }
                }

                if (sb.length() < 1) {
                    LovelyToast.makeText(NewsListActivity_BeiFen2.this, "请选择消息", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    sb.deleteCharAt(sb.length() - 1);
                    String id = sb.toString();
                    new ConnDeleteNews(id, new AsyCallBack<CommenBean>() {
                        @Override
                        public void onStart(int type) throws Exception {
                            super.onStart(type);
                        }

                        @Override
                        public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                            super.onSuccess(toast, type, o, commenBean);
                            LovelyToast.makeText(NewsListActivity_BeiFen2.this, commenBean.getMessage(), 0, LovelyToast
                                    .SUCCESS, LovelyToast.SCALE);

                            adapter.notifyDataSetChanged();
                            select = 0;

                        }

                        @Override
                        public void onFail(String toast, int type) throws Exception {
                            super.onFail(toast, type);
                        }

                        @Override
                        public void onEnd(String toast, int type) throws Exception {
                            super.onEnd(toast, type);
                        }
                    }


                    ).execute(NewsListActivity_BeiFen2.this, false);


//                    ck_al
                    ck_all.setChecked(false);
                }

                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void checkGroup(int position, boolean isChecked) {
        list.get(position).setChoosed(isChecked);

        if (isAllCheck())
            ck_all.setChecked(true);
        else
            ck_all.setChecked(false);

        adapter.notifyDataSetChanged();
    }

    private boolean isAllCheck() {

        for (BaseTable.BaseA group : list) {
            if (!group.isChoosed())
                return false;
        }
        return true;
    }

    @Override
    public void doIncrease(int position, View showCountView, boolean isChecked) {

    }

    @Override
    public void doDecrease(int position, View showCountView, boolean isChecked) {

    }

    @Override
    public void childDelete(int position) {

    }

    public void getData() {

    }

    @Override
    public void onRefresh() {


        list.clear();

        try {
            list.addAll(BaseApplication.baseDB.baseTable.query(null, null, null, null, null, "time desc", null));

            adapter = new MessageAdapter(context,select, list);
            adapter.setCheckInterface(NewsListActivity_BeiFen2.this);
            adapter.setModifyCountInterface(NewsListActivity_BeiFen2.this);


            list_shopping_cart.setAdapter(adapter);

//           shoppingCartAdapter = new NewsListTestAdapter(NewsListActivity.this, select, list);
//           list_shopping_cart.setAdapter(shoppingCartAdapter);


        } catch (Exception e) {
            e.printStackTrace();
        }

        adapter.notifyDataSetChanged();


        list_shopping_cart.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
//
                intent.putExtra("url", "http://luzhengtang.cn/index.php/api/pushinfo/info?id=" + list.get(position).push_id + "");

                intent.setClass(NewsListActivity_BeiFen2.this, NoticeWebActivity.class);

                new ConnFlagNews(list.get(position).push_id + "", new AsyCallBack<CommenBean>() {
                    @Override
                    public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                        super.onSuccess(toast, type, o, commenBean);
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);

                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {
                        super.onEnd(toast, type);
                    }
                }


                ).execute(NewsListActivity_BeiFen2.this, false);

                startActivity(intent);
            }
        });

    }
 /**
     * 遍历list集合
     *
     * @return
     */



}
